<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('organization.contract.form') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item class="mavon" prop="content" :label="i18n('organization.contract.content')">
            <editor ref="editorRef" :content="dataForm.content"></editor>
          </el-form-item>

          <el-form-item>
            <el-button
              v-if="isAuth('module:organization:contract:handle')"
              type="primary"
              @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>
            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="organization-contract-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import Editor from '@/components/form/ckeditor'

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('organization/contract')

  const editorRef = ref()

  const dataForm = reactive({
    organization_id: 0,
    content: '',
  })

  const dataRule = reactive({})

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.organization_id = query.organization_id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.organization_id) {
        $http({
          url: $http.adornUrl(`/${model.value}/data`),
          method: 'get',
          params: $http.adornParams({
            organization_id: dataForm.organization_id
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.content = data.data.content
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/handle`),
          method: 'post',
          data: $http.adornData({
            organization_id: dataForm.organization_id,
            content: editorRef.value.data,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  onMounted(() => {
    init(formRef)
  })
</script>
